package cn.wolfcode.wolf2w.message.service.impl;

import cn.wolfcode.wolf2w.common.core.exception.ServiceException;
import cn.wolfcode.wolf2w.common.core.utils.uuid.UUID;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.utils.RedisKeys;
import cn.wolfcode.wolf2w.message.service.ISmsService;
import cn.wolfcode.wolf2w.message.util.SmsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements ISmsService {

    @Autowired
    private RedisService redisService;

    @Override
    public void sendVerifyCode(String phone) {
        if(StringUtils.isBlank(phone)){
            throw new ServiceException("手机号码不能为空");
        }

//        String code = UUID.randomUUID().toString().replace("-","").substring(4);
        Random random = new Random();
        int code = random.nextInt(9000)+1000;
//        String code = Integer.toString(number);
//        SmsUtil.sendSmAli(phone,code);
        System.out.println("验证码为："+code);
        System.out.println("手机号为："+phone);

        //存储验证码
        String key = RedisKeys.REGIST_VERIFY_CODE.join(phone);
        redisService.setCacheObject(key,code, RedisKeys.REGIST_VERIFY_CODE.getTime(), TimeUnit.SECONDS);//key,验证码,有效时间,时间单位
    }
}
